<template>
  <!--会签管理-我的提交-付款申请编辑弹窗-表单-->
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="标题" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="title">
              <a-input v-model="model.title" placeholder="请输入标题"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="对方名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="other">
              <a-input v-model="model.other" placeholder=""></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
        
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="项目" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="projectId"
              style="margin-bottom: 10px;">
              <a-select :disabled="disabled"  placeholder="请选择项目" :value="model.projectName" @change="handleChange('project',$event)">
                <a-select-option v-for="(item,index) in levelOneOptions" :key="index" :value="item">
                      {{item.label}}
                    </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="银行账号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="bankAccount">
              <a-input v-model="model.bankAccount" placeholder=""></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
        
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="开户银行" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="bankDeposit">
              <a-input v-model="model.bankDeposit" placeholder=""></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="付款项目" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="paymentProject">
              <a-input v-model="model.paymentProject" placeholder=""></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
        
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="付款金额" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="paymentMoney">
              <a-input v-model="model.paymentMoney" placeholder=""></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="合同编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="contract">
              <a-input v-model="model.contract" placeholder=""></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
        
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="备注" :labelCol="labelCol2" :wrapperCol="wrapperCol2" prop="remark"
              style="margin-bottom: 10px;margin-left: 20px;">
              <textarea v-model="model.remark" cols="100" rows="3" style="width: 93%;"></textarea>
            </a-form-model-item>
          </a-col>
        </a-row>
        
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="附件资料" :labelCol="labelCol2" :wrapperCol="wrapperCol2" prop="attachment"
              style="margin:0 60px 10px 20px;">
              <j-upload v-model="model.attachment"></j-upload>
            </a-form-model-item>
          </a-col>
        </a-row>
        
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
  import {
    httpAction,
    getAction
  } from '@/api/manage'
  import {
    validateDuplicateValue
  } from '@/utils/util'

  export default {
    name: 'WtBrandForm',
    components: {},
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false,
      },
    },
    data() {
      return {
        model: {
          countersignId: '',
          projectName: '',
          attachment: '',
          // status: '1',
        },
        labelCol: {
          xs: {
            span: 24
          },
          sm: {
            span: 5
          },
        },
        wrapperCol: {
          xs: {
            span: 24
          },
          sm: {
            span: 16
          },
        },
        labelCol2: {
          xs: {
            span: 24
          },
          sm: {
            span: 2
          },
        },
        wrapperCol2: {
          xs: {
            span: 24
          },
          sm: {
            span: 22
          },
        },
        confirmLoading: false,
        // validatorRules: { paymentMoney: [{required: true,pattern: /^[0-9]{99}$/,message: '请填写金额'}]},
        levelOneOptions: [],
        treeData: [],
        templateId: '',
        treeData1: [],
        fileList: [],
        url: {
          add: '/countersign/wtCountersignPaymentRequest/add',
          edit: '/countersign/wtCountersignPaymentRequest/edit',
          anew: '/countersign/wtCountersignPaymentRequest/editSubmit',
          queryById: '/wtBrand/queryById',
        },
        pageType: 'edit', //edit-编辑 anew-重新提交
      }
    },
    computed: {
      formDisabled() {
        return this.disabled
      },
    },
    created() {
      //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model))
      // console.log('dfsa',this.countersignTemplateId);
    },
    methods: {
      handleChange(type,item){
        console.log(1,type,item);
        if(type === 'project'){
          this.model.projectName = item.label;
          this.model.projectId = item.value;
        }
      },
      loadTree(record) {
        var that = this
        getAction(`/countersign/wtCountersignPaymentRequest/queryByCountersignId?countersignId=${record.id}`).then(
          (res) => {
            if (res.success) {
              this.treeData1 = res.result

              this.model = this.treeData1
              // this.model.projectName=this.treeData1.projectId
              // this.model.projectName=this.treeData1.projectName

              getAction(`/sys/sysDepart/getDepartByOrgType?orgType=1`).then((res) => {
                if (res.success) {
                  that.treeData = []
                  let treeList = res.result
                  for (let a = 0; a < treeList.length; a++) {
                    let temp = treeList[a]
                    temp.isLeaf = temp.leaf
                    that.treeData.push(temp)
                  }

                  this.levelOneOptions = [...that.treeData].map((v) => {
                    return {
                      ...v,
                      value: v.id,
                      label: v.departName,
                    }
                  })
                  let info1 = that.levelOneOptions.find((item) => item.departName == this.model.projectName)
                  that.model.projectId = info1.id
                }
              })

              // that.model.id=that.treeData[0].id
            }
          }
        )
      },
      add() {
        this.edit(this.modelDefault)
      },
      edit(record, type) {
        // this.model = Object.assign({}, record)
        if (!!type) {
          this.pageType = type;
        }
        this.visible = true
        this.loadTree(record)
        this.templateId = record.id
      },
      handleOk() {
        const that = this
        // 触发表单验证
        // this.$refs.form.validate((valid) => {
        //   if (valid) {
        //     that.confirmLoading = true
        //     let httpurl = ''
        //     let method = ''
        //     if (!this.model.id) {
        //       httpurl += this.url.add
        //       method = 'post'
        //     } else {
        //       httpurl += this.url.edit
        //       method = 'put'
        //     }
        //     httpAction(httpurl, this.model, method)
        //       .then((res) => {
        //         if (res.success) {
        //           that.$message.success(res.message)
        //           that.$emit('ok')
        //         } else {
        //           that.$message.warning(res.message)
        //         }
        //       })
        //       .finally(() => {
        //         that.confirmLoading = false
        //       })
        //   }
        // })
      },
      submitForm(record) {
        const that = this
        if (!this.model.attachment) {
          that.$message.warning('附件资料不能为空,请上传')
        } else {
          // 触发表单验证
          this.$refs.form.validate((valid) => {
            if (valid) {
              that.confirmLoading = true
              let httpurl = ''
              if (that.pageType === 'edit') {
                httpurl = that.url.edit
              } else if (that.pageType === 'anew') {
                httpurl = that.url.anew
              }
              that.model.countersignId = that.templateId
              that.model.status = record
              that.model.list = that.model.list
              // console.log('wq',that.model);
              console.log('提交表单', that.model)
              httpAction(httpurl, that.model, 'POST')
                .then((res) => {
                  if (res.success) {
                    that.$message.success(res.message)
                    that.$emit('ok')
                  } else {
                    that.$message.warning(res.message)
                  }
                })
                .finally(() => {
                  that.confirmLoading = false
                })
            }
          })
        }
        
      },
    },
  }
</script>